﻿using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SVTTranslateTextEngine.Core
{
    /// <summary>
    /// File access auxiliary object
    /// </summary>
    public class FileAccessHelper
    {
        /// <summary>
        /// Read the context from the text file
        /// </summary>
        /// <param name="szFilePath">the file full path </param>
        /// <returns>file context as string </returns>
        public string ReadTextContext(string szFilePath)
        {
            string szResult = null;

            if (!string.IsNullOrEmpty(szFilePath) && File.Exists(szFilePath))
            {
                StreamReader srReader = null;
                try
                {
                    srReader = new StreamReader(szFilePath, Encoding.UTF8);
                    szResult = srReader.ReadToEnd();
                }
                finally
                {
                    if (srReader != null)
                    {
                        srReader.Dispose();
                        srReader.Close();
                    }
                }
            }
            return szResult;
        }

        /// <summary>
        /// Get the size of the file
        /// </summary>
        /// <param name="szFilePath">file full path</param>
        /// <returns></returns>
        public long GetTheFileSize(string szFilePath)
        {
            long size = 0;

            if (!string.IsNullOrEmpty(szFilePath) && File.Exists(szFilePath))
            {
                var sr = new StreamReader(szFilePath);
                size = sr.BaseStream.Length;
            }
            return size;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="szContext"></param>
        /// <param name="szFilePath"></param>
        /// <returns></returns>
        public bool WriteTextContext(string szContext, string szFilePath)
        {
            return WriteTextContext(szContext, szFilePath, false);
        }

        public bool WriteTextContext(string szContext, string szFilePath, bool isApp)
        {
            bool bResult = false;
            if (!string.IsNullOrEmpty(szFilePath) && szFilePath.LastIndexOf("\\") > -1)
            {
                string szDirPath = szFilePath.Substring(0, szFilePath.LastIndexOf("\\"));
                if (Directory.Exists(szDirPath))
                {
                    StreamWriter sWriter = null;
                    try
                    {
                        if (!isApp && File.Exists(szFilePath)) File.Delete(szFilePath);
                        sWriter = new StreamWriter(szFilePath,isApp,Encoding.UTF8);
                        sWriter.Write(szContext);
                        bResult = true;
                    }
                    finally
                    {
                        if (sWriter != null)
                        {
                            sWriter.Dispose();
                            sWriter.Close();
                        }
                    }
                }
            }
            return bResult;
        }

        public bool WriteBytesContext(byte[] bsContexct, string szFilePath)
        {
            bool bResult = false;
            if (!string.IsNullOrEmpty(szFilePath) && szFilePath.LastIndexOf("\\") > -1)
            {
                string szDirPath = szFilePath.Substring(0, szFilePath.LastIndexOf("\\"));
                if (Directory.Exists(szDirPath))
                {
                    FileStream fs = null;
                    try
                    {
                        fs = new FileStream(szFilePath, FileMode.Create);
                        fs.Write(bsContexct, 0, bsContexct.Length);
                        bResult = true;
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Dispose();
                            fs.Close();
                        }
                    }
                }
            }
            return bResult;
        }

        public string GetIPAddress()
        {
            string result = "";
            IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            result =
                (from IPAddress a in addressList where a.AddressFamily == AddressFamily.InterNetwork select a.ToString())
                    .First();
            return result;
        }

        public void CreateDir(string szDirPath)
        {
            var ssTemp = szDirPath.Split('\\');
            if(ssTemp.Length > 0)
            {
                var sbTempDirPath = new StringBuilder();
                foreach(var str in ssTemp)
                {
                    if(sbTempDirPath.Length>0) sbTempDirPath.Append("\\");
                    sbTempDirPath.Append(str);
                    if (!Directory.Exists(sbTempDirPath.ToString())) Directory.CreateDirectory(sbTempDirPath.ToString());
                }
            }
        }
    }
}